﻿<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Register TagPrefix="hi" Assembly="Hidistro.UI.SaleSystem.Tags" Namespace="Hidistro.UI.SaleSystem.Tags" %>
<%@ Import Namespace="Hidistro.Entities.Commodities" %>
<%@ Import Namespace="Microsoft.Practices.EnterpriseLibrary.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="Hidistro.Core" %>
<%@ Import Namespace="Hidistro.Membership.Context" %>
<%@ Import Namespace="Hidistro.SqlDal.Members" %>
<%@ Import Namespace="Hidistro.SaleSystem.Catalog" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        int categoryId;
        if (int.TryParse(this.Request.Form["categoryId"], out categoryId))
        {
            wnxd_Product_List list;
            if (categoryId == 9) list = (wnxd_Product_List)this.FindControl("product_cnxh2");
            else list = (wnxd_Product_List)this.FindControl("product_cnxh1");
            list.DataSource = this.GetCaiNiXiHuan(categoryId, 5);
            list.DataBind();
        }
    }
    private DataTable GetCaiNiXiHuan(int categoryId, int MaxNum)
    {
        if (categoryId == 9)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "NewID()",
                CategoryIds = categoryId.ToString(),
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }
        else
        {
            IList<int> browedProductList = BrowsedProductQueue.GetBrowedProductList();
            if (browedProductList.Count == 0)
            {
                SubjectListQuery query = new SubjectListQuery
                {
                    SortBy = "NewID()",
                    CategoryIds = categoryId.ToString(),
                    MaxNum = MaxNum
                };
                return ProductBrowser.GetSubjectList(query);
            }
            else
            {
                string pids = "";
                for (int i = 0; i < browedProductList.Count; i++)
                {
                    if (i > 0)
                    {
                        pids += ",";
                    }
                    pids += browedProductList[i];
                }
                Member user = HiContext.Current.User as Member;
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("select top({0}) ProductId,ThumbnailUrl220,ImageUrl7,ProductName,ProductText,MarketPrice,SkuId,Stock,MonthlySales,Review,ShowSaleCounts AS SaleCounts,CategoryUrl,", MaxNum);
                if (user != null)
                {
                    int discount = new MemberGradeDao().GetMemberGrade(user.GradeId).Discount;
                    sb.AppendFormat(" (CASE WHEN (SELECT COUNT(*) FROM Hishop_SKUMemberPrice WHERE SkuId = b.SkuId AND GradeId = {0}) = 1", user.GradeId);
                    sb.AppendFormat(" THEN (SELECT MemberSalePrice FROM Hishop_SKUMemberPrice WHERE SkuId = b.SkuId AND GradeId = {0}) ELSE SalePrice*{1}/100 END) AS RankPrice", user.GradeId, discount);
                }
                else
                {
                    sb.Append("SalePrice AS RankPrice");
                }
                sb.AppendFormat(" from vw_Hishop_BrowseProductList as b where ProductId not in ({0}) and MainCategoryPath in (select MainCategoryPath from Hishop_Products where ProductId in ({0})) order by newid() asc,Stock desc", pids);
                return this.sql_call(sb.ToString());
            }
        }
    }
    Database database = DatabaseFactory.CreateDatabase();
    private DataTable sql_call(string sql)
    {
        using (DbCommand cmd = database.GetSqlStringCommand(sql))
        using (IDataReader reader = database.ExecuteReader(cmd))
        {
            return DataHelper.ConverDataReaderToDataTable(reader);
        }
    }
</script>
